package com.kim.smartring.activity.fragment;

import java.util.Calendar;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kim.smartring.R;
import com.kim.smartring.activity.SmartRingMainActivity;
import com.kim.smartring.ble.BleNamesResolver;
import com.kim.smartring.broadcastreceiver.BLEBroadcastReceiver;
import com.kim.smartring.utils.BLEDataUtils;

/**
 * 防丢设置页
 * 
 */
public class LossFragment extends BaseFragment {
	private ImageView img_menu;
	private RelativeLayout rl_setting;// 设置超过多少米
	private ImageView img_whistle;// 哨子
	private TextView txv_loss;// 设置超过多少米就报警的文字

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.loss_layout, null);
		img_menu = (ImageView) v.findViewById(R.id.img_menu);
		rl_setting = (RelativeLayout) v.findViewById(R.id.rl_setting);
		img_whistle = (ImageView) v.findViewById(R.id.img_whistle);
		txv_loss = (TextView) v.findViewById(R.id.txv_loss);

		img_menu.setOnClickListener(this);
		rl_setting.setOnClickListener(this);
		img_whistle.setOnClickListener(this);
		return v;
	}

	@Override
	public void onResume() {
		super.onResume();
		enableLosslinkPhoneAlertNotify();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.img_menu:
			showSlidingMenu();
			break;
		case R.id.rl_setting:

			break;
		case R.id.img_whistle:
			writeLossLinkAlert();
			break;

		default:
			break;
		}
	}

	private void enableLosslinkPhoneAlertNotify() {
		SmartRingMainActivity mainActivity = null;
		try {
			mainActivity = (SmartRingMainActivity) getActivity();
			mainActivity.switchNotify(
					BleNamesResolver.UUID_LOSSLINK_PHONE_ALERT,
					BleNamesResolver.UUID_C_LOSSLINK_PHONE_ALERT, true);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

		}
	}

	private void writeLossLinkAlert() {
		SmartRingMainActivity mainActivity = null;
		try {
			mainActivity = (SmartRingMainActivity) getActivity();
			String data = BLEDataUtils.buildLossLinkAlert();
			mainActivity.writeDataToSmartRing(data,
					BleNamesResolver.UUID_LOSSLINK_ALTERT_RIGHTNOW,
					BleNamesResolver.UUID_C_LOSSLINK_ALTERT_RIGHTNOW);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {

		}
	}

}